<%@ Page language="vb" CodeFile="addPreVehicleDocumentation.aspx.vb" Inherits="addPreVehicleDocumentation_aspx_vb" %>

<%
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        qCountryID = Request.QueryString("qCountryID")
        If Len(qCountryID) = 0 Then 
            qCountryID = 0
        Else
            qCountryID = CLng(qCountryID)
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""countryID"", ""<1""],"
        If CInt(qCountryID) = 3 Then 
            strJS = strJS + "[""vinNumber"", ""==''""],"
        Else
            strJS = strJS + "[""regnr"", ""==''""],"
        End IF
        strJS = strJS + "[""preVehicleDocumentation"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>WRITE LEASE DOCUMENTATION FOR FUTURE CONTRACTS<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='addPreVehicleDocumentation.aspx?do=changeCountryID' method='post'>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Country:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='countryID' id='countryID' onchange=""submit();"">")
        Response.Write("<option value=''> Select country  ")
        dbOpen()
        RecSet = Conn.Execute("Select countryID, country From tblCountry Order By country")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                countryID = RecSet.Fields("countryID").Value
                country = RecSet.Fields("country").Value
                Response.Write("<option value='" & countryID & "'")
                If qCountryID = countryID Then 
                    Response.Write(" selected='selected'")
                End IF
                Response.Write("> " & country & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_countryID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<form action='addPreVehicleDocumentation.aspx?do=save&countryID=" & qCountryID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Licence number:</td><td align='right'><input type='text' name='regnr' id='regnr' size='40' maxlength='50'></td><td><div id='error_regnr' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>VIN-number:</td><td align='right'><input type='text' name='vinNumber' id='vinNumber' size='40' maxlength='50'></td><td><div id='error_vinNumber' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Documentation:</td></tr>")
        Response.Write("<tr>")
        Response.Write("<td colspan='2'><textarea name='preVehicleDocumentation' style='width: 100%; height: 50px'></textarea></td>")
        Response.Write("<td valign='top'><div id='error_preVehicleDocumentation' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='2'>DOCUMENTATION HISTORY<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select pvd.preVehicleDocumentationID, pvd.regnr, pvd.vinNumber, pvd.preVehicleDocumentation, pvd.date, u.username From (tblPreVehicleDocumentation pvd INNER JOIN tblUser u ON u.userID = pvd.userID) Order By pvd.preVehicleDocumentationID desc")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No documentation made</font></td></tr>")
        Else
            Do Until RecSet.EOF
                preVehicleDocumentationID = RecSet.Fields("preVehicleDocumentationID").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                preVehicleDocumentation = RecSet.Fields("preVehicleDocumentation").Value
                __date = RecSet.Fields("date").Value
                username = RecSet.Fields("username").Value
                Response.Write("<tr><td colspan='2'>" & preVehicleDocumentation & "</td></tr>")
                Response.Write("<tr>")
                Response.Write("<td><i>Created by " & username & " for licence number " & regnr & " / VIN-number " & vinNumber & " on " & Left(__date, 10) & "</i></td>")
                Response.Write("<td align='right'><a href='addPreVehicleDocumentation.aspx?do=delete&preVehicleDocumentationID=" & preVehicleDocumentationID & "'>delete</a></td>")
                Response.Write("</tr>")
                Response.Write("<tr><td><br></td></tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "changeCountryID" Then 
        countryID = Request.Form("countryID")
        Response.Redirect("addPreVehicleDocumentation.aspx?qCountryID=" & countryID & "")
    ElseIf Request.QueryString("do") = "save" Then 
        countryID = Request.QueryString("countryID")
        regnr = Request.Form("regnr")
        vinNumber = Request.Form("vinNumber")
        preVehicleDocumentation = Request.Form("preVehicleDocumentation")
        userID = fragusLeasingUserID
        countryID = CLng(countryID)
        If CInt(countryID) = 0 Then 
            Response.Redirect("addPreVehicleDocumentation.aspx?subDo=%20-%20ERROR")
        End IF
        If Len(regnr) > 0 Then 
            regnr = Trim(regnr)
            regnr = Replace(CStr(regnr), "'", "")
            regnr = Replace(CStr(regnr), """", "")
            regnr = Replace(CStr(regnr), "<", "")
            regnr = Replace(CStr(regnr), ">", "")
            regnr = Replace(CStr(regnr), "=", "")
            regnr = Replace(CStr(regnr), "&", "")
            regnr = UCase(regnr)
        Else
            regnr = "N/A"
        End IF
        If Len(vinNumber) > 0 Then 
            vinNumber = Trim(vinNumber)
            vinNumber = Replace(CStr(vinNumber), "'", "")
            vinNumber = Replace(CStr(vinNumber), """", "")
            vinNumber = Replace(CStr(vinNumber), "<", "")
            vinNumber = Replace(CStr(vinNumber), ">", "")
            vinNumber = Replace(CStr(vinNumber), "=", "")
            vinNumber = Replace(CStr(vinNumber), "&", "")
        Else
            vinNumber = "N/A"
        End IF
        If Len(preVehicleDocumentation) > 0 Then 
            preVehicleDocumentation = Trim(preVehicleDocumentation)
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), """", "&quot;")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "'", "&#39;")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), vbCrLf, "<br>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[b]", "<b>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[/b]", "</b>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[i]", "<i>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[/i]", "</i>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[u]", "<u>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[/u]", "</u>")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[c]", "<font color=""red"">")
            preVehicleDocumentation = Replace(CStr(preVehicleDocumentation), "[/c]", "</font>")
        End IF
        dbOpen()
        Conn.Execute("Insert Into tblPreVehicleDocumentation (countryID, userID, regnr, vinNumber, preVehicleDocumentation) Values(" & countryID & "," & userID & ",'" & regnr & "','" & vinNumber & "','" & preVehicleDocumentation & "')")
        dbClose()
        Response.Redirect("addPreVehicleDocumentation.aspx?subDo=%20-%20SAVED")
    ElseIf Request.QueryString("do") = "delete" Then 
        preVehicleDocumentationID = Request.QueryString("preVehicleDocumentationID")
        preVehicleDocumentationID = CLng(preVehicleDocumentationID)
        dbOpen()
        Conn.Execute("Delete From tblPreVehicleDocumentation Where preVehicleDocumentationID = " & preVehicleDocumentationID & "")
        dbClose()
        Response.Redirect("addPreVehicleDocumentation.aspx?subDo=%20-%20DELETED")
    End IF
%>
